package com.ajou.adonate.imap;

import java.io.File;
import java.util.ArrayList;

import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.internet.*;

import com.ajou.adonate.database.DatabaseManager;
import com.ajou.adonate.filemgr.*;

import Threadpool.ThreadPool;

public class MailSender extends Thread {
	
	private UserSession m_UserSession = null;
	//private Session m_TempSession = null;
	FileUploader m_UploaderThread = null;
	private int m_FileHashCode = 0;
	private final static int SIZE = 8000000;
	
	private DatabaseManager d = null;
	
	private String m_FileName = null;
	private String m_FilePath = null;
	private String m_Title = null;
	private String m_Author = null;
	private String m_Description = null;
	private String m_EmailAccount = null;
	
	private String m_UploadPath = "/Users/tapaha7/upload/";
	
	public MailSender(UserSession TempSession) {
		m_UserSession = TempSession;
		d = new DatabaseManager();
		try {
			Class.forName(d.getJdbcDriver());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void SendNewMail(String FileName, String FilePath, String title, String author, String description, String email_account) {
		m_FileName = FileName;
		m_FilePath = FilePath;
		m_Title = title;
		m_Author = author;
		m_Description = description;
		m_EmailAccount = email_account;
		start();
	}
	
	public void run()
	{
		long FileSize = 0;
		if(m_UserSession.getSession() != null && (FileSize = getFileSize(m_FilePath)) != 0) {
			int Size = 20971520;
			
			System.out.println("m_HashCode : " + m_FileHashCode);
			System.out.println("m_FilePath : " + m_FilePath);
			
//			FileSplit Fs = new FileSplit(m_UploadPath, m_FileName, Size);
//			Fs.byteToFile(m_UploadPath + "UploadTemp/", m_FileName);
//			
//			int FileCount = Fs.getFileCount();
//			ArrayList<String> fList = Fs.getFileList();
//			for(int i=0; i<Fs.getFileCount(); ++i) {
//				System.out.println("FileSplit, path = " + fList.get(i));
//			}
//			
//			ThreadPool pool = new ThreadPool(FileCount);
//			for(int i=0; i<FileCount; ++i) {
//				String SplitFilePath = fList.get(i);
//				m_UploaderThread = new FileUploader(m_UserSession);
//				m_UploaderThread.UploadNewFile(m_FileName, SplitFilePath, m_FileHashCode ,i, (int)FileSize/*, Integer.valueOf(m_DirectoryId).intValue()*/);
//				pool.runTask(m_UploaderThread);	// insert threadpool
//			}
			
			ThreadPool pool = new ThreadPool(1);
			
			m_UploaderThread = new FileUploader(m_UserSession);
			m_UploaderThread.UploadNewFile(m_FileName, m_FilePath, m_FileHashCode, (int)FileSize);
			pool.runTask(m_UploaderThread);	// insert threadpool
			
			pool.join();
			
			//Upload Complete
			
			if(d.uploadComplete(m_EmailAccount, m_Title, m_Author, m_Description, m_FileHashCode, FileSize)) {
				System.out.println("Insert Database Success.");
				
				//delete Uploaded File
				File delFile = new File(m_FilePath);
				delFile.delete();
			}
			//Upload complate
			//MailReader reader = new MailReader(m_UserSession);
			//MailContext Mc =  reader.getUnreadMailContext(m_FileName);
			
			//FileManager DbConnect = new FileManager();
			
			//if(DbConnect.uploadFile(11, Mc))
			//if(DbConnect.uploadFile(m_UserSession.getDB_MemberId(), Mc)) {
//				System.out.println("Success Send File : " + FileCount );
			//	System.out.println("Success Insert DB ");
			//}
			
			// delete temp file
//			for(int i=0; i<FileCount; ++i) {
//				File delFile = new File(fList.get(i));
//				delFile.delete();
//			}
			
			//DbConnect.clearFileCache();
			pool.close();
		} else {
			System.out.println("Error");
		}
	}
	
	private long getFileSize(String filename1){
		  File file = new File(filename1);
		  long FileSize = file.length();
		  
		  if(FileSize != 0) m_FileHashCode = file.hashCode();
		  
		  System.out.println("File Size : " + FileSize);
		  return FileSize;

	 }

}
